﻿using Infrastructure.DbUtility;
using Infrastructure.Utility;
using System;
public partial class admin_gradesubject : BasePage
{

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            this.Index = 1;
            this.LoadGradeSubject(1, string.Empty, string.Empty);
        }

        if (Request["do"] == "save") this.SaveMappingInfo();
    }

    private void LoadGradeSubject(int index, string grade, string subject)
    {
        string sql = string.Empty, filter = string.Empty;
        if (!string.IsNullOrWhiteSpace(grade)) filter = " AND B.[name]  LIKE  '%' + {0} + '%'";
        if (!string.IsNullOrWhiteSpace(subject)) filter += " AND C.[name] LIKE  '%' + {1} + '%'";

        if (filter.Length > 0) filter = filter.Substring(4);

        sql = "SELECT COUNT(1) count FROM GradeSubject A (NOLOCK) "
                                                               + " LEFT JOIN Grade B (NOLOCK) ON A.gid = B.nid "
                                                               + " LEFT JOIN Subject C (NOLOCK) ON A.sid = C.sid " + (filter == string.Empty ? string.Empty : " WHERE " + filter) + Environment.NewLine;

        if (index == 1)
        {
            sql += "SELECT TOP " + PAGE_SIZE + " A.*,B.[name] gname,C.[name] sname FROM GradeSubject A (NOLOCK) "
                                                                                                           + " LEFT JOIN Grade B (NOLOCK) ON A.gid = B.nid "
                                                                                                           + " LEFT JOIN Subject C (NOLOCK) ON A.sid = C.sid " + (filter == string.Empty ? string.Empty : " WHERE " + filter);
        }
        else
        {
            sql += "SELECT TOP " + PAGE_SIZE + " A.*,B.[name] gname,C.[name] sname FROM GradeSubject (NOLOCK) "
                                                                                     + " LEFT JOIN Grade B (NOLOCK) ON A.gid = B.nid "
                                                                                     + " LEFT JOIN Subject C (NOLOCK) ON A.sid = C.sid WHERE A.gsid NOT IN (SELECT TOP " + ((index - 1) * PAGE_SIZE) + " gsid FROM GradeSubject (NOLOCK))" + (filter == string.Empty ? string.Empty : " AND " + filter); ;
        }

        DbHelper db = new DbHelper();
        var ds = db.ExecuteQueryEx(sql, grade, subject);

        this.Index = index;
        int count = ds.Tables[0].Rows[0].Value<int>("count");
        this.TotalSize = count / PAGE_SIZE + (count % PAGE_SIZE == 0 ? 0 : 1);

        lstGradeSubject.DataSource = ds.Tables[1];
        lstGradeSubject.DataBind();
    }
    protected void btnPost_Click(object sender, EventArgs e)
    {
        string grade = txtGrade.Text.Trim();
        var subject = txtName.Text.Trim();
        this.Index = 1;
        this.LoadGradeSubject(1, grade, subject);
    }
    protected void btnNavigate_Click(object sender, EventArgs e)
    {
        string sindex = Request["txtGoTo"];
        int index = 0;
        if (!int.TryParse(sindex, out index) || index < 1) return;

        string grade = txtGrade.Text.Trim();
        var subject = txtName.Text.Trim();
        this.Index = index;

        this.LoadGradeSubject(index, grade, subject);
    }
    protected void btnDel_Click(object sender, EventArgs e)
    {
        var val = Request["selector"];
        if (string.IsNullOrWhiteSpace(val)) return;

        string sql = "DELETE FROM GradeSubject WHERE gsid IN (" + val + ")";

        DbHelper db = new DbHelper();
        db.ExecuteUpdate(sql);

        string grade = txtGrade.Text.Trim();
        var subject = txtName.Text.Trim();
        this.LoadGradeSubject(this.Index, grade, subject);
    }
    protected void SaveMappingInfo()
    {
        var sgid = Request["gid"];
        var ssid = Request["sid"];

        int gid = 0;
        int sid = 0;

        if (int.TryParse(sgid, out gid) && gid > 0 && int.TryParse(ssid, out sid) && sid > 0)
        {
            string sql = "INSERT INTO GradeSubject SELECT {0},{1} WHERE NOT EXISTS(SELECT 1 FROM GradeSubject (NOLOCK) WHERE gid={0} AND sid = {1})";
            DbHelper helper = new DbHelper();
            helper.ExecuteUpdateEx(sql, gid, sid);
        }

        Response.Clear();
        Response.Write("{\"success\": true}");
        Response.End();
    }
}